package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Sports;
import com.itheima.service.SportsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/sports")
public class SportsController {
    @Reference
    private SportsService sportsService;
    @PreAuthorize("hasAuthority('SPORTS_QUERY')")
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult=sportsService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }
    @PreAuthorize("hasAuthority('SPORTS_ADD')")
    @RequestMapping("/add")
    public Result add(@RequestBody Sports sports){
        try{
            sportsService.add(sports);
            return new Result(true,MessageConstant.SPORTS_ADD_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SPORTS_ADD_FAIL);
        }
    }
    @PreAuthorize("hasAuthority('SPORTS_QUERY')")
    @RequestMapping("/findById")
    public Sports findById(Integer id){
       return sportsService.findById(id);
    }
    @PreAuthorize("hasAuthority('SPORTS_EDIT')")
    @RequestMapping("edit")
    public Result edit(@RequestBody Sports sports){
        try{
            sportsService.edit(sports);
            return new Result(true,MessageConstant.SPORTS_EDIT_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SPORTS_EDIT_FAIL);
        }
    }
    @PreAuthorize("hasAuthority('SPORTS_DELETE')")
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try{
            sportsService.deleteById(id);
            return new Result(true,MessageConstant.SPORTS_DELETE_SUCCESS);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.SPORTS_DELETE_FAIL);
        }
    }
}
